package com.hwd.srb.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/11 10:21
 * @Description 统一返回结果集
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回内容
     */
    private String message;

    /**
     * 数据对象
     */
    private Map<String, Object> data = new HashMap<>();

    /**
     * 返回成功
     */
    public static R ok() {
        return new R().code(ResponseEnum.SUCCESS.getCode()).message(ResponseEnum.SUCCESS.getMessage());
    }

    public static R ok(String message) {
        return new R().code(ResponseEnum.SUCCESS.getCode()).message(message);
    }

    /**
     * 返回失败
     */
    public static R error() {
        return new R().code(ResponseEnum.ERROR.getCode()).message(ResponseEnum.ERROR.getMessage());
    }

    public static R error(String message) {
        return new R().code(ResponseEnum.ERROR.getCode()).message(message);
    }

    /**
     * 设置特定结果
     */
    public static R setResult(ResponseEnum responseEnum) {
        return new R().code(responseEnum.getCode()).message(responseEnum.getMessage());
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
